<template>
  <div class="personCenterPage">
    <a-card title="个人信息" style="width: 100%; margin-bottom: 20px">
      <div style="display: flex; align-items: center">
        <!-- 只有在编辑模式下才显示上传组件 -->
        <template v-if="isEditing">
          <a-upload
            :show-upload-list="false"
            :custom-request="handleUpload"
          >
            <a-avatar v-if="userInfo?.userAvatar" :size="100" :src="userInfo.userAvatar" style="margin-right: 20px" />
            <div v-else>
              <loading-outlined v-if="loading"></loading-outlined>
              <plus-outlined v-else></plus-outlined>
              <div class="ant-upload-text">点击或拖拽图片上传</div>
            </div>
            <!-- 添加编辑模式下的提示 -->
            <div class="edit-avatar-tip" v-if="isEditing">点击头像可修改</div>
          </a-upload>
        </template>
        <!-- 非编辑模式下只显示头像 -->
        <template v-else>
          <a-avatar v-if="userInfo?.userAvatar" :size="100" :src="userInfo.userAvatar" style="margin-right: 20px" />
        </template>
        <div>
          <p>ID: {{ userInfo.id }}</p>
          <p>账号名: {{ userInfo.userAccount }}</p>
          <p>
            用户名:
            <span v-if="!isEditing">{{ userInfo.userName }}</span>
            <a-input v-else v-model:value="editUserName" placeholder="请输入用户名" />
          </p>
          <p>角色: {{ roleDescription }}</p>
          <p>创建时间:{{ dayjs(userInfo.createTime).format('YYYY-MM-DD HH:mm:ss') }}</p>
        </div>
      </div>
      <div style="text-align: right; margin-top: 20px">
        <a-button v-if="!isEditing" type="primary" @click="startEdit">编辑</a-button>
        <a-button v-else type="primary" @click="saveEdit">保存</a-button>
      </div>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.js'
import dayjs from 'dayjs'
import { updateUserUsingPost, uploadResourceUsingPost1 } from '@/api/userController'
import { message } from 'ant-design-vue'

const isEditing = ref(false)
const editUserName = ref('')

const userInfo = ref({
  id: '',
  userAccount: '',
  userName: '',
  userAvatar: '',
  userRole: '',
  createTime: '',
  editTime: '',
})

const loginUserStore = useLoginUserStore()
userInfo.value = loginUserStore.loginUser

function startEdit() {
  // 初始化编辑字段为当前用户名
  editUserName.value = userInfo.value.userName
  isEditing.value = true
}

/**
 * 上传图片
 */
const handleUpload = async ({ file }: any) => {
  loading.value = true
  try {

    const res = await uploadResourceUsingPost1({file:file})
    if (res.data.code === 0 && res.data.data) {
      message.success('图片上传成功')
      userInfo.value.userAvatar = res.data.data
    } else {
      message.error('图片上传失败' + res.data.message)
    }
  } catch (error) {
    console.log('图片上传失败' + error?.message)
  } finally {
    loading.value = false
  }
}

async function saveEdit() {
  try {
    const response = await updateUserUsingPost({
      id: userInfo.value.id,
      userName: editUserName.value,
      userAvatar: userInfo.value.userAvatar, // 确保更新头像信息
      // 如果有其他需要更新的字段，可以在这里添加
    })

    if (response.data) {
      // 更新本地状态
      userInfo.value.userName = editUserName.value
      // 假设 loginUserStore.fetchLoginUser 是从后端获取最新用户信息的方法
      message.success('修改成功')
      isEditing.value = false
    } else {
      console.error('修改失败', response.data)
      message.error('修改失败')
    }
  } catch (error) {
    console.error('保存错误', error)
    message.error('保存错误')
  }
}

const loading = ref<boolean>(false)

// 计算属性，根据用户角色返回对应的中文描述
const roleDescription = computed(() => {
  if (userInfo.value.userRole === 'user') {
    return '普通用户'
  } else if (userInfo.value.userRole === 'admin') {
    return '管理员'
  }
  return userInfo.value.userRole
})
</script>

<style scoped>
.personCenterPage {
  max-width: 700px;
  margin: 0 auto;
}

.personCenterPage p {
  margin: 5px 0;
}

.personCenterPage .a-input {
  width: 200px;
}

.edit-avatar-tip {
  color: #888;
  font-size: 12px;
  text-align: center;
  margin-top: 5px;
}
</style>
